MongoDB 是一個 NoSQL 實現。NoSQL 在具有高吞吐量的應用程序中可以非常高性能。
如果數據發生變化,我們必須在多個地方更新它。我們檢索數據,不必 Join(Mysql join)多個表,雖然現在的 API 可以使用 Join 但應該盡量避免。
簡單的使用 docker-compose 建立環境
version: '3.7'
services:
mongo:
image: mongo:4
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: '00000000'
ports:
- 27017:27017
volumes:
- data-volume:/data/db
- config-volume:/data/configdb
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: '00000000'
ME_CONFIG_MONGODB_URL: mongodb://root:00000000@mongo:27017/
volumes:
data-volume:
config-volume:
在 Spring boot 的 pom.xml 加入以下
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置 Spring boot 連線 MongoDB 的資訊
@Slf4j
@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
@Value("${mongo_db}")
private String database;
@Value("${mongo_hosts}")
private String hosts;
@Value("${mongo_ports}")
private String ports;
@Value("${mongo_user_name}")
private String user;
@Value("${mongo_password}")
private String password;
@Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
return database;
}
@Override
public MongoClient mongoClient() {
// TODO Auto-generated method stub
// 如果有配置使用者以及帳號密碼
MongoCredential credential = MongoCredential.createScramSha256Credential(user, database, password.toCharArray());
// createScramSha256Credential(user, database, password.toCharArray());
List<ServerAddress> serverAddresses = new ArrayList<>();
List<String> hostList = Arrays.asList(hosts.split(","));
List<String> portList = Arrays.asList(ports.split(","));
for (String host : hostList) {
Integer index = hostList.indexOf(host);
Integer port = Integer.parseInt(portList.get(index));
ServerAddress serverAddress = new ServerAddress(host, port);
serverAddresses.add(serverAddress);
}
MongoClient mongoClients = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder -> builder.hosts(serverAddresses))
.credential(credential)
.build());
log.info("Mongodb Server Addresses: {}", serverAddresses.toString());
log.info("Mongo Client: {}", mongoClients.getDatabase(database));
/**
* 從 database 獲取 gnss collection
*/
// MongoDatabase database = mongoClients.getDatabase(getDatabaseName());
// MongoCollection<Document> coll = database.getCollection("gnss");
// coll.find().forEach(d -> System.out.println(d.toJson()));
return mongoClients;
}
@Override
protected Collection<String> getMappingBasePackages() {
// TODO Auto-generated method stub
return Collections.singleton("com.example.cch.mongo");
}
@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(), getDatabaseName());
}
}
在 application.properties 配置環境變數
mongo_db=aiot
mongo_hosts=172.17.8.222
#
mongo_ports=27017
#
mongo_user_name=root
mongo_password=00000000
# spring.data.mongodb.auto-index-creation=true
將以下註解打開即可測試 Spring boot 是否能對 MongoDB 進行交互
MongoDatabase database = mongoClients.getDatabase(getDatabaseName());
MongoCollection<Document> coll = database.getCollection("gnss");
coll.find().forEach(d -> System.out.println(d.toJson()));
mongo-express 是一個可以操作 MongoDB 的 WebGUI 介面不論是創建 index 或是 collection 都相當方便~